package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.courser.Util;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.OaTaskDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.OaTask;
import com.prs.crm.domain.oa.OaTaskSource;

public class OaTaskDaoImpl  extends BaseDaoImpl<OaTask> implements OaTaskDao{

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object[]> getTaskReportDataSouce(String state,
			Date datestart, Date dateend, Collection<Person> persons) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(OaTask.class);
		if(state != null)
			criteria.add(Restrictions.eq("state", state));
		if(datestart != null)
			criteria.add(Restrictions.ge("beginTime", datestart));
		if(dateend != null)
			criteria.add(Restrictions.le("beginTime", dateend));
		if(persons != null)
			criteria.add(Restrictions.in("recorder", persons));
		
		ProjectionList projectionList = Projections.projectionList();
		projectionList.add(Projections.groupProperty("recorder"));
		projectionList.add(Projections.rowCount());
		criteria.setProjection(projectionList);
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTask> getChildTask(final OaTask oaTask) {
		// TODO Auto-generated method stub
		return (Collection<OaTask>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTask.class);
				criteria.add(Restrictions.eq("upTask", oaTask));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTask> getChildTask(final OaTask oaTask, final Person person) {
		// TODO Auto-generated method stub
		return (Collection<OaTask>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTask.class);
				criteria.add(Restrictions.eq("upTask", oaTask));
				criteria.add(Restrictions.eq("recorder", person));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTask> getValidCritiqueOaTask(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<OaTask>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTask.class);
				criteria.add(Restrictions.ne("state", Constrains.TASK_PASS));
				criteria.createAlias("copyToPersons", "c").add(Restrictions.eq("c.id", person.getId()));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTask> getOverdueOaTask(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<OaTask>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTask.class);
				criteria.createAlias("submits", "s");
				criteria.add(Restrictions.eq("s.exePerson", person));
				criteria.add(Restrictions.eq("state", Constrains.TASK_EXECUTE));
				criteria.add(Restrictions.lt("endTime", Util.nowFullTime()));
				criteria.addOrder(Order.asc("endTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	public List<OaTask> getAllTask() {
		Criteria criteria = this.getSession().createCriteria(OaTask.class);
		return criteria.list();
	}

}
